package sk.fiit.vppj.eventmanager.data;

import java.util.Collections;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import sk.fiit.vppj.eventmanager.core.DatabaseManager;

/**
 * Data class, which represents organization. The class is immutable.
 * @author Marek
 *
 */
public final class Organization {

	private Map<String, Object> data = null;
	private int id = 0;
	private final static Logger log = Logger.getLogger( Organization.class.getName() );

	public Organization(int id) {
		setData( id );
		this.id = id;
		if( this.data == null ) {
			log.log( Level.INFO, "Selected organization is not in database! id = ?", id );
			throw new NullPointerException( "Organization was not set!" );
		}
	}

	private void setData(int id) {
		final String sql = "SELECT jos_users.name AS bossName, jos_event_manager_organizations.*, "
				+ " jos_event_manager_states.name AS stateName FROM jos_event_manager_organizations JOIN jos_users ON "
				+ " jos_users.id = jos_event_manager_organizations.user_id JOIN jos_event_manager_states ON "
				+ " jos_event_manager_states.IDState = jos_event_manager_organizations.state_id WHERE IDOrganization = ?";

		this.data = Collections.unmodifiableMap( DatabaseManager.getInstance().getJdbcTemplate().queryForMap( sql, new Object[] { id } ) );
	}

	public String getBossName() {
		return this.data.get( "bossName" ).toString();
	}

	public String getName() {
		return this.data.get( "organizationName" ).toString();
	}

	public String getAddress() {
		return this.data.get( "organizationAddress" ).toString();
	}

	public String getCity() {
		return this.data.get( "organizationCity" ).toString();
	}

	public String getZip() {
		return this.data.get( "organizationZip" ).toString();
	}

	public String getState() {
		return this.data.get( "stateName" ).toString();
	}

	public String getPhone() {
		return this.data.get( "organizationPhone" ).toString();
	}

	public String getWebAddress() {
		return this.data.get( "organizationWeb" ).toString();
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + id;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if( this == obj )
			return true;
		if( obj == null )
			return false;
		if( getClass() != obj.getClass() )
			return false;
		Organization other = ( Organization ) obj;
		if( id != other.id )
			return false;
		return true;
	}

}
